//
//  HMEmoticonKeyboardView.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: - 设置视图
    private func setupUI(){
    backgroundColor = UIColor(patternImage: UIImage(named:"emoticon_keyboard_background" )!)
        //增加控件
        addSubview(emoticonbottomView)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        //控件约束
        emoticonbottomView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        emoticonPageView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emoticonbottomView.snp_top)
        }
        pageControl.snp_makeConstraints { (make) in
            make.bottom.equalTo(emoticonPageView).offset(5)
            make.centerX.equalTo(emoticonPageView)
            
        }
    
        //底部视图闭包回调
        emoticonbottomView.closure = {(type:HMEmoticonBottomViewType) -> () in

//            switch type {
//            case .Default:
//                print("确认")
//            case .Emoji:
//                print("emoji")
//            case .Lxh:
//                print("浪小花")
//            }
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //滚动到指定页
            self.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Right, animated:false)
            
            //设置Pagecontrol
            self.setupControl(indexPath)
 
        }
        //设置Pagecontrol
        
        setupControl(NSIndexPath(forItem: 0, inSection: 0))

    }
     // MARK: - 懒加载控件
    //底部视图
    private lazy var emoticonbottomView : HMEmoticonBottomView = HMEmoticonBottomView(frame:CGRectZero)
    // 底部表情
    private lazy var emoticonPageView : HMEmoticonPageView = {
        let view = HMEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        //分页
        view.pagingEnabled = true
        //取消弹簧
        view.bounces = false
        //隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        //设置代理
        view.delegate = self
        return view
    }()
    //pageControl
    private lazy var pageControl:UIPageControl = {
        let view = UIPageControl()

        //默认颜色
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named:"compose_keyboard_dot_normal" )!)
        //只用KVC 给只读属性赋值（6.4以后就不再开放了 但是可以使用）
        view.setValue(UIImage(named:"compose_keyboard_dot_normal" ), forKey: "pageImage")
        //选中颜色
//        view.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named:"compose_keyboard_dot_selected" )!)
        view.setValue(UIImage(named:"compose_keyboard_dot_selected" ), forKey: "currentPageImage")
        //取消用户交互
        view.userInteractionEnabled = false
        return view
    }()
}

extension HMEmoticonKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获取中心点x（相对于OageView）
        let centerX = SCREENW * 0.5 + scrollView.contentOffset.x
        //获取屏幕显示cell (由于系统不帮我们排序 ，需要自动排序)
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2 ) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        //如果屏幕显示俩个Cell才会判断
        if cells.count == 2{
            //第一个cell
            let firstCell = cells.first!
            //最后一个cell
            let lastCell = cells.last!
            //定义一个indexpath
            let indexPath: NSIndexPath
            //判断中心点 是否在第一个cell中
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
//                print("第一个cell显示多")
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
            }else{
//                print("第二个cell显示多多")
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
                
            }
            //设置选中该的按钮
            emoticonbottomView.serupCurrentBtn(indexPath.section)
            
            //设置Pagecontrol
           setupControl(indexPath)
        }
    }
        //设置Pagecontrol
    func setupControl(indexPath:NSIndexPath){
    
        //总页数
        pageControl.numberOfPages = HMEmoticonTool.sharedTools.allEmoticons[indexPath.section].count
        //选中页
        pageControl.currentPage = indexPath.item

    }
    
}
